package com.example.hqpatrol;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.example.hqpatrol.base.BaseActivity;
import com.example.hqpatrol.base.BaseFragment;
import com.example.hqpatrol.databinding.ActivityMainBinding;
import com.example.hqpatrol.fragment.PatrolFinishedFragment;
import com.example.hqpatrol.fragment.PatrolHomeFragment;
import com.example.hqpatrol.fragment.PatrolReadyFragment;
import com.example.hqpatrol.fragment.PatrolTimeoutFragment;
import com.google.android.material.bottomnavigation.BottomNavigationItemView;
import com.google.android.material.bottomnavigation.BottomNavigationMenuView;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;
import com.xuexiang.xpage.base.XPageActivity;
import com.xuexiang.xui.adapter.FragmentAdapter;
import com.xuexiang.xui.utils.ResUtils;
import com.xuexiang.xui.utils.SnackbarUtils;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.utils.XToastUtils;
import com.xuexiang.xui.widget.dialog.DialogLoader;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xui.widget.imageview.RadiusImageView;
import com.xuexiang.xui.widget.textview.badge.Badge;
import com.xuexiang.xui.widget.textview.badge.BadgeView;
import com.xuexiang.xutil.XUtil;
import com.xuexiang.xutil.common.ClickUtils;
import com.xuexiang.xutil.common.CollectionUtils;
import com.xuexiang.xutil.display.Colors;

public class MainActivity extends BaseActivity<ActivityMainBinding> implements Toolbar.OnMenuItemClickListener, ClickUtils.OnClick2ExitListener {

    private String[] mTitles;
    public static PatrolReadyFragment patrolReadyFragment;
    private PatrolFinishedFragment patrolFinishedFragment;
    private PatrolTimeoutFragment patrolTimeoutFragment;
    public static PatrolHomeFragment patrolHomeFragment;

    public static Badge badge_ready;

    @Nullable
    @Override
    protected ActivityMainBinding viewBindingInflate(LayoutInflater inflater) {
        return ActivityMainBinding.inflate(inflater);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    private void initView() {
//        WidgetUtils.clearActivityBackground(this);

        //顶部工具栏
        mTitles = ResUtils.getStringArray(R.array.home_titles);
        binding.includeMain.toolbar.setTitle(mTitles[0]);
        binding.includeMain.toolbar.inflateMenu(R.menu.menu_main);
        binding.includeMain.toolbar.findViewById(R.id.action_search).setVisibility(View.GONE);
        binding.includeMain.toolbar.setOnMenuItemClickListener(this);

        //主页填充
        patrolReadyFragment = new PatrolReadyFragment();
        patrolFinishedFragment = new PatrolFinishedFragment();
        patrolTimeoutFragment = new PatrolTimeoutFragment();
        patrolHomeFragment = new PatrolHomeFragment();
        BaseFragment[] fragments = new BaseFragment[]{patrolHomeFragment, patrolReadyFragment, patrolFinishedFragment, patrolTimeoutFragment};
        FragmentAdapter<BaseFragment> adapter = new FragmentAdapter<>(getSupportFragmentManager(), fragments);
        binding.includeMain.viewPager.setOffscreenPageLimit(mTitles.length -1);
        binding.includeMain.viewPager.setAdapter(adapter);

        //侧滑抽屉
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, binding.drawerLayout, binding.includeMain.toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        binding.drawerLayout.addDrawerListener(toggle);
        toggle.syncState();

        //抽屉内头部内容
        View navHeaderView = binding.navView.getHeaderView(0);
        navHeaderView.setBackgroundColor(Colors.WHITE);
        ((RadiusImageView)navHeaderView.findViewById(R.id.iv_avatar)).setImageResource(R.drawable.ic_default_head);
        ((TextView)navHeaderView.findViewById(R.id.tv_avatar)).setText("小明");
        ((TextView)navHeaderView.findViewById(R.id.tv_sign)).setText("吃好喝好");

        //抽屉内点击事件
        binding.navView.setNavigationItemSelectedListener(menuItem -> {
            int id = menuItem.getItemId();
            if(R.id.nav_settings == id){

            }else if(R.id.nav_about == id){

            }else{
                SnackbarUtils.Short(binding.navView, menuItem.getTitle().toString()).confirm().show();
            }
            return true;
        });

        //主页面改变事件
        binding.includeMain.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                MenuItem item = binding.includeMain.bottomNavigation.getMenu().getItem(position);
                binding.includeMain.toolbar.setTitle(item.getTitle());
                item.setChecked(true);

                if(position == 1){
                    binding.includeMain.toolbar.findViewById(R.id.action_search).setVisibility(View.VISIBLE);
                    patrolReadyFragment.onShowPage();
                }else{
                    binding.includeMain.toolbar.findViewById(R.id.action_search).setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        //底部菜单栏选中事件
        binding.includeMain.bottomNavigation.setOnItemSelectedListener(item -> {
            int index = CollectionUtils.arrayIndexOf(mTitles, item.getTitle());
            if(index >= 0){
                binding.includeMain.toolbar.setTitle(item.getTitle());
                binding.includeMain.viewPager.setCurrentItem(index);
                return true;
            }
            return false;
        });

        //初始化待巡检角标
        BottomNavigationMenuView bottomNavigationMenuView = (BottomNavigationMenuView)binding.includeMain.bottomNavigation.getChildAt(0);
        BottomNavigationItemView itemView = (BottomNavigationItemView)bottomNavigationMenuView.getChildAt(1);
        badge_ready = new BadgeView(this).bindTarget(itemView.findViewById(R.id.nav_ready));
        badge_ready.setGravityOffset(20, 0, true);

    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        int id = item.getItemId();
        if(id == R.id.action_privacy){
            DialogLoader.getInstance().showTipDialog(this, "提示", "你点击了隐私政策", "确定");
        }else if(id == R.id.action_search){
            patrolReadyFragment.toggleSearchDropDown();
        }
        return false;
    }

    // 按两次返回键退出

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            ClickUtils.exitBy2Click(2000, this);
        }
        return true;
    }

    @Override
    public void onRetry() {
        XToastUtils.toast("再按一次退出程序");
    }

    @Override
    public void onExit() {
        XUtil.exitApp();
    }
}